(function() {
  var sis = window.IconStorage = {
    getIcons: function() {
      var icons = localStorage.getItem('shortcut_icons') || '{}';
      return JSON.parse(icons);
    },

    updateIcons: function(icons) {
      icons = JSON.stringify(icons);
      localStorage.setItem('shortcut_icons', icons);
    },

    addIcon: function(iconId, iconFile, iconIndex) {
      var icons = sis.getIcons();
      if (!icons[iconId] && !sis.isExist(iconFile, iconIndex)) {
        icons[iconId] = {
          file: iconFile,
          index: iconIndex
        };
        sis.updateIcons(icons);
      }
    },

    getIcon: function(iconId) {
      var icons = sis.getIcons();
      return icons[iconId] || null;
    },

    deleteIcon: function(iconId) {
      var icons = sis.getIcons();
      if (icons[iconId]) {
        delete icons[iconId];
        sis.updateIcons(icons);
      }
    },

    isExist: function(file, index) {
      var icons = sis.getIcons();
      for (var id in icons) {
        if (icons[id].file == file && icons[id].index == index)
          return id;
      }
      return false;
    }
  };
})();